//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "RangeAccrualFloatersCoupon.h"
using namespace Cephei::QL::Cashflows;
#include <gen/QL/Times/Schedule.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Indexes/IborIndex.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Indexes/InterestRateIndex.h>
#include <gen/QL/Cashflows/FloatingRateCouponPricer.h>
#include <gen/QL/Cashflows/FloatingRateCoupon.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Indexes;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Cashflows::CRangeAccrualFloatersCoupon::CRangeAccrualFloatersCoupon (DateTime paymentDate, Double nominal, Cephei::QL::Indexes::IIborIndex^ index, DateTime startDate, DateTime endDate, UInt32 fixingDays, Cephei::QL::Times::IDayCounter^ dayCounter, Double gearing, Double spread, DateTime refPeriodStart, DateTime refPeriodEnd, Cephei::QL::Times::ISchedule^ observationsSchedule, Double lowerTrigger, Double upperTrigger, Cephei::QL::Cashflows::IFloatingRateCouponPricer^ QL_Pricer) : CFloatingRateCoupon(CRangeAccrualFloatersCoupon::typeid)
{
    CIborIndex^ _Cindex;
    CDayCounter^ _CdayCounter;
    CSchedule^ _CobservationsSchedule;
    try
    {
#ifdef HANDLE
        _phRangeAccrualFloatersCoupon = NULL;
#endif
        QuantLib::Date _paymentDate = (QuantLib::Date)ValueHelper::Convert (paymentDate);
        QuantLib::Real _nominal = (QuantLib::Real)ValueHelper::Convert (nominal);
        _Cindex = safe_cast<CIborIndex^> (index);
        _Cindex->Lock();
        boost::shared_ptr<QuantLib::IborIndex>& _index = static_cast<boost::shared_ptr<QuantLib::IborIndex>&> (_Cindex->GetShared ()); 
        QuantLib::Date _startDate = (QuantLib::Date)ValueHelper::Convert (startDate);
        QuantLib::Date _endDate = (QuantLib::Date)ValueHelper::Convert (endDate);
        QuantLib::Natural _fixingDays = (QuantLib::Natural)ValueHelper::Convert (fixingDays);
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        QuantLib::Real _gearing = (QuantLib::Real)ValueHelper::Convert (gearing);
        QuantLib::Rate _spread = (QuantLib::Rate)ValueHelper::Convert (spread);
        QuantLib::Date _refPeriodStart = (QuantLib::Date)ValueHelper::Convert (refPeriodStart);
        QuantLib::Date _refPeriodEnd = (QuantLib::Date)ValueHelper::Convert (refPeriodEnd);
        _CobservationsSchedule = safe_cast<CSchedule^> (observationsSchedule);
        _CobservationsSchedule->Lock();
        boost::shared_ptr<QuantLib::Schedule>& _observationsSchedule = static_cast<boost::shared_ptr<QuantLib::Schedule>&> (_CobservationsSchedule->GetShared ()); 
        QuantLib::Real _lowerTrigger = (QuantLib::Real)ValueHelper::Convert (lowerTrigger);
        QuantLib::Real _upperTrigger = (QuantLib::Real)ValueHelper::Convert (upperTrigger);
        _ppRangeAccrualFloatersCoupon = new boost::shared_ptr<QuantLib::RangeAccrualFloatersCoupon> (new QuantLib::RangeAccrualFloatersCoupon ( _paymentDate,  _nominal,  _index,  _startDate,  _endDate,  _fixingDays,  _dayCounter,  _gearing,  _spread,  _refPeriodStart,  _refPeriodEnd,  _observationsSchedule,  _lowerTrigger,  _upperTrigger ));
        CFloatingRateCouponPricer^ _CQL_Pricer = safe_cast<CFloatingRateCouponPricer^> (QL_Pricer);
        boost::shared_ptr<QuantLib::FloatingRateCouponPricer>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::FloatingRateCouponPricer>&> (_CQL_Pricer->GetShared ());
        (*_ppRangeAccrualFloatersCoupon)->setPricer (_QL_Pricer);
        SetFloatingRateCoupon (boost::dynamic_pointer_cast<QuantLib::FloatingRateCoupon> (*_ppRangeAccrualFloatersCoupon));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cindex != nullptr) _Cindex->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
        if (_CobservationsSchedule != nullptr) _CobservationsSchedule->Unlock();
    }
}
Cephei::QL::Cashflows::CRangeAccrualFloatersCoupon::CRangeAccrualFloatersCoupon (boost::shared_ptr<QuantLib::RangeAccrualFloatersCoupon>& childNative, Object^ owner) : CFloatingRateCoupon(CRangeAccrualFloatersCoupon::typeid)
{
#ifdef HANDLE
	_phRangeAccrualFloatersCoupon = NULL;
#endif
	_ppRangeAccrualFloatersCoupon = &childNative;
    _ppFloatingRateCoupon = new boost::shared_ptr<QuantLib::FloatingRateCoupon> (boost::dynamic_pointer_cast<QuantLib::FloatingRateCoupon> (*_ppRangeAccrualFloatersCoupon));
}
Cephei::QL::Cashflows::CRangeAccrualFloatersCoupon::CRangeAccrualFloatersCoupon (QuantLib::RangeAccrualFloatersCoupon& childNative, Object^ owner) : CFloatingRateCoupon(CRangeAccrualFloatersCoupon::typeid)
{
#ifdef HANDLE
	_phRangeAccrualFloatersCoupon = NULL;
#endif
	_ppRangeAccrualFloatersCoupon = new boost::shared_ptr<QuantLib::RangeAccrualFloatersCoupon> (&childNative);
    _ppFloatingRateCoupon = new boost::shared_ptr<QuantLib::FloatingRateCoupon> (boost::dynamic_pointer_cast<QuantLib::FloatingRateCoupon> (*_ppRangeAccrualFloatersCoupon));
    _RangeAccrualFloatersCouponOwner = owner;
    _FloatingRateCouponOwner = owner;
}

Cephei::QL::Cashflows::CRangeAccrualFloatersCoupon::CRangeAccrualFloatersCoupon (CRangeAccrualFloatersCoupon^ copy) : CFloatingRateCoupon(CRangeAccrualFloatersCoupon::typeid)
{
#ifdef HANDLE
	_phRangeAccrualFloatersCoupon = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppRangeAccrualFloatersCoupon = new boost::shared_ptr<QuantLib::RangeAccrualFloatersCoupon> (copy->GetShared());
        _ppFloatingRateCoupon = new boost::shared_ptr<QuantLib::FloatingRateCoupon> (boost::dynamic_pointer_cast<QuantLib::FloatingRateCoupon> (*_ppRangeAccrualFloatersCoupon));
    }
}
Cephei::QL::Cashflows::CRangeAccrualFloatersCoupon::CRangeAccrualFloatersCoupon (System::Type^ t) : CFloatingRateCoupon(CRangeAccrualFloatersCoupon::typeid)
{
#ifdef HANDLE
	_phRangeAccrualFloatersCoupon = NULL;
#endif
	if (!t->IsSubclassOf(CRangeAccrualFloatersCoupon::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Cashflows::CRangeAccrualFloatersCoupon::CRangeAccrualFloatersCoupon (QuantLib::Handle<QuantLib::RangeAccrualFloatersCoupon>& childNative, Object^ owner)  : CFloatingRateCoupon(CRangeAccrualFloatersCoupon::typeid)
{
	_phRangeAccrualFloatersCoupon = &childNative;
	_ppRangeAccrualFloatersCoupon = &static_cast<boost::shared_ptr<QuantLib::RangeAccrualFloatersCoupon>>(childNative.currentLink());
    _ppFloatingRateCoupon = new boost::shared_ptr<QuantLib::FloatingRateCoupon> (boost::dynamic_pointer_cast<QuantLib::FloatingRateCoupon> (*_ppRangeAccrualFloatersCoupon));
    _RangeAccrualFloatersCouponOwner = owner;
}
Cephei::QL::Cashflows::CRangeAccrualFloatersCoupon::CRangeAccrualFloatersCoupon (QuantLib::Handle<QuantLib::RangeAccrualFloatersCoupon> childNative)  : CFloatingRateCoupon(CRangeAccrualFloatersCoupon::typeid)
{
	_phRangeAccrualFloatersCoupon = &childNative;
	_ppRangeAccrualFloatersCoupon = &static_cast<boost::shared_ptr<QuantLib::RangeAccrualFloatersCoupon>>(childNative.currentLink());
    _ppFloatingRateCoupon = new boost::shared_ptr<QuantLib::FloatingRateCoupon> (boost::dynamic_pointer_cast<QuantLib::FloatingRateCoupon> (*_ppRangeAccrualFloatersCoupon));
}
#endif
#ifdef STRUCT
Cephei::QL::Cashflows::CRangeAccrualFloatersCoupon::CRangeAccrualFloatersCoupon (QuantLib::RangeAccrualFloatersCoupon childNative)  : CFloatingRateCoupon(CRangeAccrualFloatersCoupon::typeid)
{
#ifdef HANDLE
	_phRangeAccrualFloatersCoupon = NULL;
#endif
	_ppRangeAccrualFloatersCoupon = new boost::shared_ptr<QuantLib::RangeAccrualFloatersCoupon> (new QuantLib::RangeAccrualFloatersCoupon (childNative));
    _ppFloatingRateCoupon = new boost::shared_ptr<QuantLib::FloatingRateCoupon> (boost::dynamic_pointer_cast<QuantLib::FloatingRateCoupon> (*_ppRangeAccrualFloatersCoupon));
}
#endif

Cephei::QL::Cashflows::CRangeAccrualFloatersCoupon::~CRangeAccrualFloatersCoupon ()
{
    if (_ppRangeAccrualFloatersCoupon != NULL)
    {
	    delete _ppRangeAccrualFloatersCoupon;
        _ppRangeAccrualFloatersCoupon = NULL;
    }
}
Cephei::QL::Cashflows::CRangeAccrualFloatersCoupon::!CRangeAccrualFloatersCoupon ()
{
    if (_ppRangeAccrualFloatersCoupon != NULL)
    {
	    delete _ppRangeAccrualFloatersCoupon;
    }
}
QuantLib::RangeAccrualFloatersCoupon& Cephei::QL::Cashflows::CRangeAccrualFloatersCoupon::GetReference ()
{
    if (_ppRangeAccrualFloatersCoupon == NULL) throw gcnew NativeNullException ();
	return **_ppRangeAccrualFloatersCoupon;
}
boost::shared_ptr<QuantLib::RangeAccrualFloatersCoupon>& Cephei::QL::Cashflows::CRangeAccrualFloatersCoupon::GetShared ()
{
    if (_ppRangeAccrualFloatersCoupon == NULL) throw gcnew NativeNullException ();
	return *_ppRangeAccrualFloatersCoupon;
}
QuantLib::RangeAccrualFloatersCoupon* Cephei::QL::Cashflows::CRangeAccrualFloatersCoupon::GetPointer ()
{
    if (_ppRangeAccrualFloatersCoupon == NULL) throw gcnew NativeNullException ();
	return &**_ppRangeAccrualFloatersCoupon;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::RangeAccrualFloatersCoupon>& Cephei::QL::Cashflows::CRangeAccrualFloatersCoupon::GetHandle ()
{
	if (_phRangeAccrualFloatersCoupon == NULL)
	{
		_phRangeAccrualFloatersCoupon = new Handle<QuantLib::RangeAccrualFloatersCoupon> (*_ppRangeAccrualFloatersCoupon);
	}
	return *_phRangeAccrualFloatersCoupon;
}
#endif
bool Cephei::QL::Cashflows::CRangeAccrualFloatersCoupon::HasNative () 
{
	return (_ppRangeAccrualFloatersCoupon != NULL);
}

Double Cephei::QL::Cashflows::CRangeAccrualFloatersCoupon::EndTime::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppRangeAccrualFloatersCoupon)->endTime ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CRangeAccrualFloatersCoupon::LowerTrigger::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppRangeAccrualFloatersCoupon)->lowerTrigger ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<DateTime>^ Cephei::QL::Cashflows::CRangeAccrualFloatersCoupon::ObservationDates::get ()
{
    try
    {
    	std::vector<QuantLib::Date>& _rv = (std::vector<QuantLib::Date>&)(*_ppRangeAccrualFloatersCoupon)->observationDates ( );   
        Cephei::IVector<DateTime>^ _nrv = gcnew CoVector<DateTime> (gcnew CDateTimeVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Cashflows::CRangeAccrualFloatersCoupon::ObservationsNo::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppRangeAccrualFloatersCoupon)->observationsNo ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::ISchedule^ Cephei::QL::Cashflows::CRangeAccrualFloatersCoupon::ObservationsSchedule::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::Schedule> _rv = (boost::shared_ptr<QuantLib::Schedule>)(*_ppRangeAccrualFloatersCoupon)->observationsSchedule ( );   
        Cephei::QL::Times::CSchedule^ _nrv = gcnew Cephei::QL::Times::CSchedule (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Double>^ Cephei::QL::Cashflows::CRangeAccrualFloatersCoupon::ObservationTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Real>& _rv = (std::vector<QuantLib::Real>&)(*_ppRangeAccrualFloatersCoupon)->observationTimes ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CRangeAccrualFloatersCoupon::PriceWithoutOptionality (Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve)
{
    CYieldTermStructure^ _CdiscountCurve;
    try
    {
        _CdiscountCurve = safe_cast<CYieldTermStructure^> (discountCurve);
        _CdiscountCurve->Lock();
        Handle<QuantLib::YieldTermStructure>& _discountCurve = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CdiscountCurve->GetHandle ()); 
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppRangeAccrualFloatersCoupon)->priceWithoutOptionality ( _discountCurve );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CdiscountCurve != nullptr) _CdiscountCurve->Unlock();
    }
}
Double Cephei::QL::Cashflows::CRangeAccrualFloatersCoupon::StartTime::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppRangeAccrualFloatersCoupon)->startTime ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CRangeAccrualFloatersCoupon::UpperTrigger::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppRangeAccrualFloatersCoupon)->upperTrigger ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Cashflows::IRangeAccrualFloatersCoupon^ Cephei::QL::Cashflows::CRangeAccrualFloatersCoupon_Factory::Create (DateTime paymentDate, Double nominal, Cephei::QL::Indexes::IIborIndex^ index, DateTime startDate, DateTime endDate, UInt32 fixingDays, Cephei::QL::Times::IDayCounter^ dayCounter, Double gearing, Double spread, DateTime refPeriodStart, DateTime refPeriodEnd, Cephei::QL::Times::ISchedule^ observationsSchedule, Double lowerTrigger, Double upperTrigger, Cephei::QL::Cashflows::IFloatingRateCouponPricer^ QL_Pricer)
{
    return gcnew CRangeAccrualFloatersCoupon ( paymentDate,  nominal,  index,  startDate,  endDate,  fixingDays,  dayCounter,  gearing,  spread,  refPeriodStart,  refPeriodEnd,  observationsSchedule,  lowerTrigger,  upperTrigger,  QL_Pricer);
}
